package com.keyue.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.dto.admin.AdminRoleDeptDO;
import com.keyue.domain.vo.admin.AdminRoleDeptQueryVO;
import com.keyue.domain.vo.admin.AdminRoleDeptVO;
import com.keyue.mapper.admin.AdminRoleDeptMapper;
import com.keyue.service.admin.AdminRoleDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 角色-部门表(AdminRoleDept)表服务实现类
 *
 * @author makejava
 * @since 2023-08-30 17:51:48
 */
@Slf4j
@Service
public class AdminRoleDeptServiceImpl extends ServiceImpl<AdminRoleDeptMapper, AdminRoleDeptDO> implements AdminRoleDeptService {


    /**
     * 通过ID查询单条数据
     * <p>
     * adminRoleDeptQueryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(AdminRoleDeptQueryVO adminRoleDeptQueryVO) {
        if (Objects.isNull(adminRoleDeptQueryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminRoleDeptDO AdminRoleDeptDo = getById(adminRoleDeptQueryVO.getId());
        if (Objects.isNull(AdminRoleDeptDo)) {
            return ServerResult.success(null);
        }
        return ServerResult.success(BeanUtil.copyProperties(AdminRoleDeptDo, AdminRoleDeptVO.class));
    }

    /**
     * 分页查询
     *
     * @param adminRoleDeptQueryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult list(AdminRoleDeptQueryVO adminRoleDeptQueryVO) {


        return ServerResult.success();
    }

    /**
     * 新增数据
     *
     * @param adminRoleDeptVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(AdminRoleDeptVO adminRoleDeptVO) {
        //todo 校验
        if (Objects.nonNull(adminRoleDeptVO.getId())) {
            adminRoleDeptVO.setId(null);
        }
        AdminRoleDeptDO adminRoleDeptDo = BeanUtil.copyProperties(adminRoleDeptVO, AdminRoleDeptDO.class);

        save(adminRoleDeptDo);

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param adminRoleDeptVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(AdminRoleDeptVO adminRoleDeptVO) {
        //todo 校验
        if (Objects.isNull(adminRoleDeptVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminRoleDeptDO adminRoleDeptDo = BeanUtil.copyProperties(adminRoleDeptVO, AdminRoleDeptDO.class);

        updateById(adminRoleDeptDo);

        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param adminRoleDeptQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(AdminRoleDeptQueryVO adminRoleDeptQueryVO) {
        List<Long> ids = adminRoleDeptQueryVO.getIds();
        if (CollUtil.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<AdminRoleDeptDO> adminRoleDeptList = listByIds(ids);
        if (CollUtil.isEmpty(adminRoleDeptList)) {
            return ServerResult.success();
        }
        //todo 校验是否可以逻辑删除
        removeByIds(ids);
        return ServerResult.success();
    }
}
